<?php
class ControllerDelimanCapnhatkybaodagiao extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
		
		$this->data['trangthai'] = array(
									'dagiao' => 'Đã giao',
									'chuagiao' => 'Chưa giao',
									'huy' => 'Hủy',
								);
		
	 	$this->load->model("deliman/khachhang");
		$this->load->model("common/control");
		$this->load->model("deliman/khuvuc");
		$this->load->model("deliman/nhanvien");
		$this->load->model("deliman/kybao");
		
		$this->data['khuvucs'] = array();
		$this->model_deliman_khuvuc->getTree(0, $this->data['khuvucs']);
   	}
	
	public function index()
	{

		$this->getThongTinDatBaoList();

	}
	
	//hàm lấy khu vực
	/*public function getTungKhuVuc($id)
	{
		$khuvucphutrachs=$this->model_deliman_nhanvien->getPhuTrachKhuVucList(" AND nhanvienphutrachid='".$id."'");
		
		
		
		$arr = array();
		foreach($khuvucphutrachs as $khuvucphutrach)
		{
			$arr[]=$khuvucphutrach['khuvucid'];
			
		}
		$where = " AND khuvucid IN ('". implode("','",$arr) ."')";
		
		
		$this->data['khuvucdangphutrachs']=$this->model_deliman_khuvuc->getList($where);
		
	}*/
	
	//lay thong tin ky bao load vao cb ky bao	
	public function getkybao()
	{
		$this->load->model("deliman/kybao");	
		$this->load->model("common/control");
		
		$loaibaoid = $this->request->get['loaibaoid'];
		$select =  $this->request->get['select'];
		
		$kybaos = $this->model_deliman_kybao->getList(" AND loaibaoid='".$loaibaoid."'");
		$this->data['output'] = "<option value=''>----Chọn tất cả----</option>".$this->model_common_control->getDataCombobox($kybaos,"tenkybao","kybaoid",$select);
		
		$this->template='common/output.tpl';
		$this->render();
	}
	
	public function getKhuVuc()
	{
		$data=$this->request->post;
		$this->data['khuvucs']=array();
		$this->model_deliman_khuvuc->getTree($data['khuvucid'],$this->data['khuvucs']);
		$this->data['output'] = json_encode($this->data['khuvucs']);
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}

	public function getnhanvien()
	{
		//lay id nhan vien quan ly khu vuc khi dang nhap vao he thong
		$nhanvienphancongid = $this->user->getNhanVien();
		
		$khuvucid = $this->request->get['khuvucid'];
		$arr_khuvucid = array();
		if($khuvucid == "")
		{
			//lay khu vuc no ql len
			$where = " AND nhanvienphutrachid = '".$nhanvienphancongid."'";
			$data_khuvuc = $this->model_deliman_khuvuc->getList($where);
			$arr_khuvucid = $this->string->matrixToArray($data_khuvuc,'khuvucid');
		}
		else
		{
			$arr_khuvucid[] = $khuvucid;
		}
		//$select =  $this->request->get['select'];
		$khuvus = array();
		foreach($arr_khuvucid as $khuvucid)
		{
			$data_temp = array();
			$this->model_deliman_khuvuc->getTree($khuvucid, $data_temp);
			$khuvus = array_merge_recursive($khuvus,$data_temp);
		}
		
		$arrkhuvucid = $this->string->matrixToArray($khuvus,'khuvucid');
		$where = " AND khuvucid in ('".implode("','",$arrkhuvucid)."') AND tinhtrang <> 'deleted'";
	
		$phutrackhuvucs = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where);
		$listnhanvienphutrachid = $this->string->matrixToArray($phutrackhuvucs,'nhanvienphutrachid');
		//print_r($listnhanvienphutrachid);
		$where = " AND nhanvienid in ('".implode("','",$listnhanvienphutrachid)."')";
		
	/*	$arr_phutrachkhachhang = $this->model_deliman_nhanvien->getPhuTrachKhachHangList(" AND nhanvienphancongid = '".$nhanvienphancongid."'");
		//lay nhung id cua nhan vien phu trach khach hang trong table phu trach khach hang
		$arr_ptkhnhanvienphutrachid = $this->string->matrixToArray($arr_phutrachkhachhang,'nhanvienphutrachid');*/
		
		//$where = "AND nhanvienid in ('".implode("','",$arr_ptkhnhanvienphutrachid)."')";
		
		$where .= " AND loainhanvienid = 3";
		$nhanviens = $this->model_deliman_nhanvien->getList($where);
		
		$this->data['output'] = "<option value=''>----Chọn tất cả----</option>".$this->model_common_control->getDataCombobox($nhanviens, "hoten", "nhanvienid", $select);
		
		$this->template='common/output.tpl';

		$this->render();
	}
	
	private function getThongTinDatBaoList() 
	{
		//ngay phat hanh max dua theo id loai bao
		$arrmaxdate = $this->model_deliman_kybao->getItemMaxDate();
		$this->data['maxdates'] = $arrmaxdate;
		
		
		//lay du lieu truyen qua form
		$data = $this->request->get;
		
		//loc khu vuc theo user dang nhap
		$nhanvienphutrachid = $this->user->getNhanVien();
		$listkhuvucid = $this->model_deliman_nhanvien->getPhuTrachKhuVucList(" AND nhanvienphutrachid='".$nhanvienphutrachid."' AND tinhtrang <> 'deleted'");
		$arrkhuvucid = $this->string->matrixToArray($listkhuvucid,'khuvucid');

		$this->data['dskhuvuc']=array();
		
		$dskhuvucid=$arrkhuvucid;
		
		foreach($dskhuvucid as $khuvucid)
		{
			$dskv=array();
			$this->model_deliman_khuvuc->getTree($khuvucid,$dskv);
			foreach($dskv as $kv)
			{
				array_push($this->data['dskhuvuc'], $kv);
			}
		}
		$this->data['khuvuc'];
		
			//lấy thông tin đặt báo có điều kiện
			$where = "";
			$this->data['datas'] = array();
			
			
			
			
		
			
			
	
	//		$data_cbkhachhang = $this->model_deliman_khachhang->getList(" AND nhanvienphutrachid in( '".implode("','",$arrnhanvienphutrachid)."')");
//			
//			$arrkhachhangid = $this->string->matrixToArray($data_cbkhachhang,'khachhangid');
			
			
			
			
			
			//loc theo dieu kien nhan vien
			//$this->data['dieukienloc'] = 0;
			
				if (trim($data['tungay']) != "")
				{
					$where .= " AND ngayphathanh >= '".$this->date->formatViewDate($data['tungay'])."'";
				}
				if (trim($data['denngay']) != "")
				{
					$where .= " AND ngayphathanh <= '".$this->date->formatViewDate($data['denngay'])."'";
				}
				if($data['loaibaoid'] != "")
				{
					$where .= " AND loaibaoid = '".$data['loaibaoid']."'";
				}
				if($data['kybaoid'] != "")
				{
					$where .= " AND kybaoid = '".$data['kybaoid']."'";
				}
				if($data['nhanvienid'] != "")
				{
					$where .= " AND nhanviengiaoid = '".$data['nhanvienid']."'";
				}
				if(($data['nhanvienid'] == null || $data['nhanvienid'] == "" ) )
				{
				
					$nhanvienid=$this->user->getNhanVien();
					$loainhanvienid=$this->user->getNhanVien('loainhanvienid');
					$where_khuvuc=$this->getLoaiNhanVien();
					$arr_khuvucid=array();
					
					if($data['khuvucid']==null || $data['khuvucid']=="0")
					{
						
						$data_khuvucquanly = $this->model_deliman_khuvuc->getList($where_khuvuc);
						foreach($data_khuvucquanly as $khuvuc)
							{
								$this->model_deliman_khuvuc->getTree($khuvuc['khuvucid'], $arr_khuvucid);
								
							}
					}
					else
					{
						$this->model_deliman_khuvuc->getTree($data_khuvucid['khuvucid'],$arr_khuvucid);
					}
					$this->data['phutrachkhuvuc']=$arr_khuvucid;
					$arr_khuvucquanly=$this->string->matrixToArray($this->data['phutrachkhuvuc'],'khuvucid');
					
					$where_khuvucphutrach= " AND khuvucid in ('".implode("','",$arr_khuvucquanly)."') AND tinhtrang <> 'deleted' ";
					$data_khuvucphutrachid=$this->model_deliman_nhanvien->getPhuTrachKhuVucList($where_khuvucphutrach);
					
					$arr_nhanvienid=$this->string->matrixToArray($data_khuvucphutrachid,'nhanvienphutrachid');
					$where_nhanvvienphatbao= " AND nhanvienid in ('".implode("','",$arr_nhanvienid)."') AND loainhanvienid='3' ";
					$nhanvienphatbao=$this->model_deliman_nhanvien->getList($where_nhanvvienphatbao);
					$arr_nhanvienphatbao=$this->string->matrixToArray($nhanvienphatbao,'nhanvienid');
					$where.=" AND nhanviengiaoid in ('".implode("','",$arr_nhanvienphatbao)."')";
				
			}
			
				
				/*//lay danh sach khach hang trong bang khach hang bo vao table thong tin dat bao
				$arrlistkhachhangid = $this->model_deliman_khachhang->getList(" AND nhanvienphutrachid = '".$data['nhanvienid']."'");
				$arrkhachhangid = $this->string->matrixToArray($arrlistkhachhangid,'khachhangid');
				$where.=" AND khachhangid in( '".implode("','",$arrkhachhangid)."')";
				
				//echo $where;
				//$this->data['dieukienloc'] = 1;
				$where .= " AND nhanviengiaoid = '".$data['nhanvienid']."'";*/
				$where.="order by ngayphathanh desc";
				$rows = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
		
				



		//cb loai bao
		$this->load->model("deliman/loaibao");
		$loaibaos = $this->model_deliman_loaibao->getList();
		$this->data['loaibao'] = "<option value=''>----Chọn tất cả----</option>".$this->model_common_control->getDataCombobox($loaibaos, "tenloaibao", "loaibaoid", $select);
		
		
		//load thong tin cap nhat ky bao
		$this->data['insert'] = $this->url->http('deliman/khachhang/insert');
		$this->data['delete'] = $this->url->http('deliman/khachhang/delete');		
		

		//load danh sach id nhan vien phu trach trong table phu trach khach hang
		
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->load->model("deliman/kybao");
			$kybao = $this->model_deliman_kybao->getItem($this->data['datas'][$i]['kybaoid']);
			//load thong tin khach hang
			$khachhang = $this->model_deliman_khachhang->getItem($this->data['datas'][$i]['khachhangid']);
			$this->data['datas'][$i]['hoten'] = $khachhang['hoten'];
			$this->data['datas'][$i]['diachi'] = $khachhang['diachi'];
			$this->data['datas'][$i]['email'] = $khachhang['email'];
			$this->data['datas'][$i]['dienthoai'] = $khachhang['dienthoai'];
			
			//load thong tin nhan vien
			$nhanvien = $this->model_deliman_nhanvien->getItem($this->data['datas'][$i]['nhanviengiaoid']);
			$this->data['datas'][$i]['hotennhanvien'] = $nhanvien['hoten'];	
		}
		//print_r($this->data['datas']);
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/capnhatkybaodagiao.tpl";
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	public function getLoaiNhanVien()
	{
		$nhanvienid=$this->user->getNhanVien();
		$loainhanvienid=$this->user->getNhanVien('loainhanvienid');
		if($loainhanvienid=="1"){
			$where = " AND loaikhuvuc = 'tinhthanh'";
		
		}
		else{
			$where = " AND nhanvienphutrachid = '".$nhanvienid."'";
		}
				
		return $where;			
	}
	
	
	
	public function updatethongtindatbao()
	{
		$data = $this->request->post;
		
		//print_r($data);		
		$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'trangthai',$data['trangthai']);
		$arr = array(
					'id'=>$data['id'],
					'trangthai' => $data['trangthai']
					);
		$this->data['output'] = json_encode($arr);
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		 
		
		if (trim($data['tenphieuphanphoi']) == "")
		{
      		$this->error['tenphieuphanphoi'] = "Bạn chưa nhập tên chức năng";
    	}
	
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	
}
?>